package com.baizhi.controller;

import com.baizhi.util.ValidateCodeUtils;
import org.patchca.service.Captcha;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;

@RequestMapping("code")
@Controller
public class CodeController {

    @RequestMapping("selectCode")
    public void selectCode(HttpServletResponse resp, HttpSession session) throws IOException {
        Captcha captcha = ValidateCodeUtils.getCaptcha();
        String content = captcha.getChallenge();
        System.out.println(content);
        BufferedImage image =captcha.getImage();
        session.setAttribute("vc", content);
        resp.setContentType("image/png");
        resp.setHeader("cache", "no-chache");
        OutputStream out = resp.getOutputStream();
        ImageIO.write(image, "png", out);
    }
}
